#include "errno.h"
#include "base.h"
#include "duct_errno.h"

static const int linux_to_darwin[512] = {

	[LINUX_EPERM] = EPERM,
	[LINUX_ENOENT] = ENOENT,
	[LINUX_ESRCH] = ESRCH,
	[LINUX_EINTR] = EINTR,
	[LINUX_EIO] = EIO,
	[LINUX_ENXIO] = ENXIO,
	[LINUX_ENOEXEC] = ENOEXEC,
	[LINUX_EBADF] = EBADF,
	[LINUX_ECHILD] = ECHILD,
	[LINUX_EDEADLK] = EDEADLK,
	[LINUX_ENOMEM] = ENOMEM,
	[LINUX_EACCES] = EACCES,
	[LINUX_EFAULT] = EFAULT,
	[LINUX_ENOTBLK] = ENOTBLK,
	[LINUX_EBUSY] = EBUSY,
	[LINUX_EEXIST] = EEXIST,
	[LINUX_EXDEV] = EXDEV,
	[LINUX_ENODEV] = ENODEV,
	[LINUX_ENOTDIR] = ENOTDIR,
	[LINUX_EISDIR] = EISDIR,
	[LINUX_EINVAL] = EINVAL,
	[LINUX_ENFILE] = ENFILE,
	[LINUX_EMFILE] = EMFILE,
	[LINUX_ENOTTY] = ENOTTY,
	[LINUX_ETXTBSY] = ETXTBSY,
	[LINUX_EFBIG] = EFBIG,
	[LINUX_ENOSPC] = ENOSPC,
	[LINUX_ESPIPE] = ESPIPE,
	[LINUX_EROFS] = EROFS,
	[LINUX_EMLINK] = EMLINK,
	[LINUX_EPIPE] = EPIPE,
	[LINUX_EDOM] = EDOM,
	[LINUX_ERANGE] = ERANGE,
	[LINUX_EAGAIN] = EAGAIN,
	[LINUX_EINPROGRESS] = EINPROGRESS,
	[LINUX_EALREADY] = EALREADY,
	[LINUX_ENOTSOCK] = ENOTSOCK,
	[LINUX_EDESTADDRREQ] = EDESTADDRREQ,
	[LINUX_EMSGSIZE] = EMSGSIZE,
	[LINUX_EPROTOTYPE] = EPROTOTYPE,
	[LINUX_ENOPROTOOPT] = ENOPROTOOPT,
	[LINUX_EPROTONOSUPPORT] = EPROTONOSUPPORT,
	[LINUX_ESOCKTNOSUPPORT] = ESOCKTNOSUPPORT,
	[LINUX_ENOTSUP] = ENOTSUP,
	[LINUX_EPFNOSUPPORT] = EPFNOSUPPORT,
	[LINUX_EAFNOSUPPORT] = EAFNOSUPPORT,
	[LINUX_EADDRINUSE] = EADDRINUSE,
	[LINUX_EADDRNOTAVAIL] = EADDRNOTAVAIL,
	[LINUX_ENETDOWN] = ENETDOWN,
	[LINUX_ENETUNREACH] = ENETUNREACH,
	[LINUX_ENETRESET] = ENETRESET,
	[LINUX_ECONNABORTED] = ECONNABORTED,
	[LINUX_ECONNRESET] = ECONNRESET,
	[LINUX_ENOBUFS] = ENOBUFS,
	[LINUX_EISCONN] = EISCONN,
	[LINUX_ENOTCONN] = ENOTCONN,
	[LINUX_ESHUTDOWN] = ESHUTDOWN,
	[LINUX_ETOOMANYREFS] = ETOOMANYREFS,
	[LINUX_ETIMEDOUT] = ETIMEDOUT,
	[LINUX_ECONNREFUSED] = ECONNREFUSED,
	[LINUX_ELOOP] = ELOOP,
	[LINUX_ENAMETOOLONG] = ENAMETOOLONG,
	[LINUX_EHOSTDOWN] = EHOSTDOWN,
	[LINUX_EHOSTUNREACH] = EHOSTUNREACH,
	[LINUX_ENOTEMPTY] = ENOTEMPTY,
	[LINUX_EUSERS] = EUSERS,
	[LINUX_EDQUOT] = EDQUOT,
	[LINUX_ESTALE] = ESTALE,
	[LINUX_EREMOTE] = EREMOTE,
	[LINUX_ENOLCK] = ENOLCK,
	[LINUX_ENOSYS] = ENOSYS,
	[LINUX_EOVERFLOW] = EOVERFLOW,
	[LINUX_ECANCELED] = ECANCELED,
	[LINUX_EIDRM] = EIDRM,
	[LINUX_ENOMSG] = ENOMSG,
	[LINUX_EILSEQ] = EILSEQ,
	[LINUX_EBADMSG] = EBADMSG,
	[LINUX_EMULTIHOP] = EMULTIHOP,
	[LINUX_ENODATA] = ENODATA,
	[LINUX_ENOLINK] = ENOLINK,
	[LINUX_ENOSR] = ENOSR,
	[LINUX_ENOSTR] = ENOSTR,
	[LINUX_EPROTO] = EPROTO,
	[LINUX_ETIME] = ETIME,
	[LINUX_EOPNOTSUPP] = EOPNOTSUPP,
};

int errno_linux_to_bsd(int err)
{
	int v = err;
	if (v < 0)
		v = -v;

	if (linux_to_darwin[v])
	{
		v = linux_to_darwin[v];
		return (err < 0) ? -v : v;
	}

	return err;
}

